﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;
namespace TranTekBE.Maintenance
{
    public partial class Allrequest : PageRequireLogin
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            func abc = new func();
            /*
             * Số dòng hiễn thị trên 1 page
             */

            int limitrow = 10;
            /*
            * Yêu cầu xem
            */

            int process = -1;
            string _process = Request.QueryString["process"];
            if (_process != null && _process.Length > 0)
                process = int.Parse(_process);


            /*
             * Lấy page hiện tại 
             */
            int currPage = 1;
            string page = Request.QueryString["page"];
            if(page != null)
                currPage = int.Parse(page);
           
            /*
             * Số lượng yêu cầu
             */
            string sqlnum = "SELECT * FROM tbl_YeuCau";
            if (process == 0)
                sqlnum = "SELECT * FROM tbl_YeuCau WHERE IDTinhTrang = 1";
            if (process == 1)
                sqlnum = "SELECT * FROM tbl_YeuCau WHERE IDTinhTrang = 2";
            if (process == 2)
                sqlnum = "SELECT * FROM tbl_YeuCau WHERE IDTinhTrang = 3";
            DataSet countrequest = FunctionsUtils.Database2.ExecuteQuery(sqlnum);
            int numrow = countrequest.Tables[0].Rows.Count;

            /*
             * Tính số trang
             */

            int _page = (numrow + (limitrow - 1)) / limitrow;

            /*
            * Lấy danh sách tất cả các yêu cầu ( id, tiêu đề, nội dung, thời gian... )
            */
            string sql = "SELECT TOP " + limitrow + " * FROM tbl_YeuCau WHERE ID NOT IN (SELECT TOP " + (currPage - 1) * limitrow + " ID FROM tbl_YeuCau ORDER BY ID DESC) ORDER BY ID DESC";
            if (process == 0)
                sql = "SELECT TOP " + limitrow + " * FROM tbl_YeuCau WHERE ID NOT IN (SELECT TOP " + (currPage - 1) * limitrow + " ID FROM tbl_YeuCau WHERE IDTinhTrang = 1 ORDER BY ID DESC) AND IDTinhTrang = 1 ORDER BY ID DESC";
            if (process == 1)
                sql = "SELECT TOP " + limitrow + " * FROM tbl_YeuCau WHERE ID NOT IN (SELECT TOP " + (currPage - 1) * limitrow + " ID FROM tbl_YeuCau WHERE IDTinhTrang = 2 ORDER BY ID DESC) AND IDTinhTrang = 2 ORDER BY ID DESC";
            if (process == 2)
                sql = "SELECT TOP " + limitrow + " * FROM tbl_YeuCau WHERE ID NOT IN (SELECT TOP " + (currPage - 1) * limitrow + " ID FROM tbl_YeuCau WHERE IDTinhTrang = 3 ORDER BY ID DESC) AND IDTinhTrang = 3 ORDER BY ID DESC";
            
            
            DataSet alllistrequest = FunctionsUtils.Database2.ExecuteQuery(sql);
            if (alllistrequest != null)
            {
                foreach (DataRow Rows in alllistrequest.Tables[0].Rows)
                {
                    allrequest.Text += "<tr>";
                    allrequest.Text += "<td>" + Rows[4].ToString() + "</td>";
                    allrequest.Text += "<td>" + abc.getPhongBan(Rows[2].ToString()) + "</td>";
                    allrequest.Text += "<td>" + abc.getCuaHang(Rows[10].ToString()) + "</td>";
                    allrequest.Text += "<td>" + Rows[1].ToString() + "</td>";
                    allrequest.Text += "<td>" + abc.getCapDo(Rows[6].ToString()) + "</td>";
                    allrequest.Text += "<td><img src=\"img/" + Rows[7].ToString() + ".png\" title=\"" + abc.getTinhTrang(Rows[7].ToString()) + "\" alt=\"" + abc.getTinhTrang(Rows[7].ToString()) + "\" width=\"16\" height=\"16\">" + /*getTinhTrang(Rows[7].ToString()) +*/ "</td>";
                    allrequest.Text += "<td class=\"textcenter\"><a href=\"Viewrequest.aspx?rid=" + Rows[0].ToString() + "\" class=\"btn btn-inverse\">Xem</a></td>";
                    allrequest.Text += "</tr>";
                }
            }

            /*
             *  Hiễn thị select page
             */

            if (currPage == 1)
            {
                pageselect.Text = "<li class=\"prev disabled\"><a>← Trang trước</a></li>";
                pageselect.Text += "<li class=\"prev disabled\"><a>" + currPage + "</a></li>";
                if (_page > currPage)
                {
                    pageselect.Text += "<li class=\"next\"><a href=\"Allrequest.aspx?page=2&process=" + _process + "\">Trang sau →</a></li>";
                }
                else
                {
                    pageselect.Text += "<li class=\"next disabled\"><a>Trang sau →</a></li>";
                }
            }
            else
            {
                pageselect.Text = "<li class=\"prev\"><a href=\"Allrequest.aspx?page=" + (currPage - 1) + "&process=" + _process + "\">← Trang trước</a></li>";
                pageselect.Text += "<li class=\"prev disabled\"><a>" + currPage + "</a></li>";
                if (_page > currPage)
                {
                    pageselect.Text += "<li class=\"next\"><a href=\"Allrequest.aspx?page=" + (currPage + 1) + "&process=" + _process + "\">Trang sau →</a></li>";
                }
                else
                {
                    pageselect.Text += "<li class=\"next disabled\"><a>Trang sau →</a></li>";
                }
            }
        }

        protected void addnew_Click(object sender, EventArgs e)
        {
            Response.Redirect("Add.aspx");
        }

        protected void seeall_Click(object sender, EventArgs e)
        {
            Response.Redirect("Maintenance.aspx");
        }
        

    }
}